<?php

namespace Trois\Crontab;

use App\HttpController\Open\WorkWeixinDsf\Event\RelayAgainMsg;
use App\HttpController\Open\WorkWeixinDsf\Event\SuiteTicketEvent;
use EasySwoole\Component\Singleton;
use EasySwoole\Crontab\JobInterface;
use EasySwoole\EasySwoole\Logger;

/**
 * 企业客服重发信息
 */
class WorkWechatDsfReplayAgainMsgCrontab implements JobInterface
{
    use Singleton;

    public function jobName(): string
    {
        return "WorkWechatDsfReplayAgainMsgCrontab";
    }

    public function crontabRule(): string
    {
        return '0 */1 * * *';
    }

    public function run(): void
    {
        $h = date('H');
        $h = $h * 1;

        if (in_array($h, [10, 15, 20])) {
            $this->handle();
        }

    }

    public function handle(): bool
    {

        Logger::getInstance()->info("开始处理 WorkWechatDsfReplayAgainMsgCrontab");

        $suites = \EasySwoole\EasySwoole\Config::getInstance()->getConf('WorkWeixinDsf');

        foreach ($suites as $suite) {

            if (!$suite['IsWork']) {
                continue;
            }

            $this->handleMsg($suite);

        }

        return true;
    }

    public function handleMsg(array $suite)
    {
        $RelayAgainMsg = new RelayAgainMsg(new SuiteTicketEvent($suite), $suite['CorpID']);
        $RelayAgainMsg->handle();
    }


    public function onException(\Throwable $throwable)
    {
        Logger::getInstance()->error("WorkWechatDsfReplayAgainMsgCrontab onException:" + $throwable->getMessage());
    }
}